<?php
class ControllerMyplanViewplan extends Controller{
	public function index(){
		$this->load->model("myplan/plan");
	
		//get the viewer identify.
		if(isset($this->request->get["user_id"])&&$this->request->get["user_id"]!=$this->user->getId()){
			$this->load->model("myplan/user");
			
			$this->data["authority_addcomment"]="yes";
			$this->data["authority_showuserdata"]="yes";
			$this->data["from_user_id"]=$this->user->getId();
			$this->data["user_id"]=$this->request->get["user_id"];
			$this->data["user"]=$this->model_myplan_user->getUserById($this->request->get["user_id"]);
			$this->data["back"]=$this->url->link("myplan/checkplan")."&user_id=".$this->request->get["user_id"];
			$this->data["action_addcomment"]=$this->url->link("myplan/viewplan/addcomment");
		}else{
			$this->data["authority_addnote"]="yes";
			$this->data["authority_checkin"]="yes";
			$this->data["back"]=$this->url->link("myplan/myplanhome");
			$this->data["action_addnote"]=$this->url->link("myplan/viewplan/addnote");
		}
	
		if($this->request->get["plan_id"]){	
			$plan=$this->model_myplan_plan->getPlan($this->request->get["plan_id"]);
				$plan_types=$this->model_myplan_plan->getPlanTypes();
				$plan_status=$this->model_myplan_plan->getStatus();		
			if($plan['type_id']==2){
				$this->initeverydayplan($plan);
			}
		}
		
		foreach($plan_types as $type){
			if($type["type_id"]==$plan["type_id"]){
				$plan["type"]=$type["type_content"];
				break;
			}
		}
		
		foreach($plan_status as $status){
			if($status["status"]==$plan["status"]){
				$plan["status_content"]=$status["status_content"];
				break;
			}
		}
		
		$this->data["plan"]=$plan;
		
		$this->data["notes"]=$this->model_myplan_plan->getPlanNote($plan["plan_id"]);
		$this->data["comments"]=$this->model_myplan_plan->getPlanComment($plan["plan_id"]);
		
		$this->data["action_checkplan"]=$this->url->link("myplan/checkplan")."&user_id=";
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplan/viewplan.tpl')) 		            {
				$this->template = $this->config->get('config_template') . '/template/myplan/viewplan.tpl';
			} else {
				$this->template = 'default/template/myplan/viewplan.tpl';
			}
			
			$this->children = array(
			'myplancommon/footer',
			'myplancommon/header'
			);
											
			$this->response->setOutput($this->render());
	}

	public function initeverydayplan($plan){
	
		$plan_checkins=$this->model_myplan_plan->getPlanCheckIn($this->request->get["plan_id"]);			
			$date=substr($plan["date_added"],0,10);
			$date_complete=substr($plan["date_complete"],0,10);
			$today=date("Y-m-d");
			$allcheckins=array();
			while($date!=date("Y-m-d",strtotime ($today."+1 day"))&&$date!=date("Y-m-d",strtotime ($date_complete."+1 day"))){
			
				$ischeck=false;
				foreach($plan_checkins as $plan_checkin){				
					if($date==$plan_checkin["date_checkin"]){
						$plan_checkin["status"]=1;
						$allcheckins[]=$plan_checkin;
						$ischeck=true;
						break;
					}
				}
				
				if(!$ischeck){
					$plan_checkin=array();
					$plan_checkin['date_checkin']=$date;
					$plan_checkin['status']=0;
					$allcheckins[]=$plan_checkin;
				}
				
				$date=date("Y-m-d",strtotime ($date."+1 day"));
			}
			
			$istodaycheck=true;
			foreach($plan_checkins as $plan_checkin){				
					if($today==$plan_checkin["date_checkin"]){
						$istodaycheck=false;
						break;
					}
			}
			if($istodaycheck&&$plan["status"]!=4&&$plan["status"]!=5){
				$this->data["checkin"]=$this->url->link("myplan/viewplan/checkin");
			}
			
			$this->data["allcheckins"]=$allcheckins;
			$this->data["today"]=$today;
	}
	
	public function checkin(){
		if(isset($this->request->post["plan_id"])&&$this->validate()){
		
			$this->load->model("myplan/plan");
			$plan_types=$this->model_myplan_plan->addCheckIn($this->request->post);
			
			$this->session->data["success"]="签到成功.";
			
			$this->redirect($this->url->link("myplan/viewplan")."&plan_id=".$this->request->post["plan_id"]);
		}
		
		$this->redirect($this->url->link("myplan/viewplan")."&plan_id=".$this->request->post["plan_id"]);
	}
	
	public function addnote(){
	
			if(isset($this->request->post["plan_id"])&&$this->validateNote()){
		
			$this->load->model("myplan/plan");
			$plan_types=$this->model_myplan_plan->addNote($this->request->post);
			
			$this->session->data["success"]="添加笔记成功.";
			
			$this->redirect($this->url->link("myplan/viewplan")."&plan_id=".$this->request->post["plan_id"]);
		}
		
		$this->redirect($this->url->link("myplan/viewplan")."&plan_id=".$this->request->post["plan_id"]);
	}
	
	public function addcomment(){
	
		if(isset($this->request->post["plan_id"])&&$this->validateComment()){
			
			$data=$this->request->post;
			
			$this->load->model("myplan/plan");
			$plan_types=$this->model_myplan_plan->addComment($data);
			
			$this->session->data["success"]="添加评论成功.";
			
		}
		
		$this->redirect($this->url->link("myplan/viewplan")."&user_id=".$this->request->post["user_id"]."&plan_id=".$this->request->post["plan_id"]);
		
	}
	
	public function validate(){
		if($this->request->post["checkin_note"]==""){
			$this->session->data["warning"]="还是做点笔记吧";
			return false;
		}
	    return true;
	}
	
	public function validateNote(){
		if($this->request->post["note_content"]==""){
			$this->session->data["warning"]="亲，不能为空哦.";
			return false;
		}
	    return true;
	}
	
	public function validateComment(){
		if($this->request->post["comment_content"]==""){
			$this->session->data["warning"]="亲，不能为空哦.";
			return false;
		}
	    return true;
	}
}
?>
